﻿#region

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class Report : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetChartType();
                panelReport1.Visible = false;
                btnHide.Visible = false;
                panelReport2.Visible = false;
                btnHide2.Visible = false;
                panelReport3.Visible = false;
                btnHide3.Visible = false;
            }
        }

        private void GetChartType()
        {
            foreach (int chartValue in Enum.GetValues(typeof (SeriesChartType)))
            {
                ListItem listItem = new ListItem(Enum.GetName(typeof (SeriesChartType), chartValue),
                    chartValue.ToString());
                ddlReportChart1.Items.Add(listItem);
            }
        }

        protected void btnReport1_OnClick(object sender, EventArgs e)
        {
            panelReport1.Visible = true;
            btnHide.Visible = true;
            LoadData();
            ddlReportChart1.SelectedValue = ((int) SeriesChartType.Column).ToString();
        }

        private void LoadData()
        {
            Series series = chart1.Series["Series"];
            DataSet ds = ReportBLL.GetReportAboutHiredSubsidiary(Convert.ToInt32(ddlCount.SelectedValue),
                ddlOrder.SelectedValue, Convert.ToInt32(ddlDays.SelectedValue));
            foreach (DataTable dt in ds.Tables)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    byte cityId = SubsidiaryBLL.GetCityIdBySubsidiaryId(Convert.ToInt32(dr["SubsidiaryId"]));
                    series.Points.AddXY(CityBLL.GetCityById(cityId).Name, Convert.ToInt32(dr["NumberOfVehicle"]));
                }
            }
        }

        protected void ddlReportChart1_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            chart1.Series["Series"].ChartType =
                (SeriesChartType) Enum.Parse(typeof (SeriesChartType), ddlReportChart1.SelectedValue);
            LoadData();
        }

        protected void btnHide_OnClick(object sender, EventArgs e)
        {
            panelReport1.Visible = false;
            btnHide.Visible = false;
        }

        protected void btnHide2_OnClick(object sender, EventArgs e)
        {
            panelReport2.Visible = false;
            btnHide2.Visible = false;
        }

        protected void btnHide3_OnClick(object sender, EventArgs e)
        {
            panelReport3.Visible = false;
            btnHide3.Visible = false;
        }

        protected void btnReport2_OnClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCusId.Text)) return;
            panelReport2.Visible = true;
            btnHide2.Visible = true;
            LoadCustomerReport();
        }

        private void LoadCustomerReport()
        {
            DataSet ds = ReportBLL.GetReportAboutCustomerHire(Convert.ToInt32(txtCusId.Text));
            DGVCustomer.DataSource = ds;
            DGVCustomer.DataBind();
        }

        protected void DGVCustomer_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DGVCustomer.PageIndex = e.NewPageIndex;
            LoadCustomerReport();
        }

        protected void DGVInactiveCustomer_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DGVInactiveCustomer.PageIndex = e.NewPageIndex;
            LoadInActiveCustomerReport();
        }

        protected void btnReport3_OnClick(object sender, EventArgs e)
        {
            panelReport3.Visible = true;
            btnHide3.Visible = true;
            LoadInActiveCustomerReport();
        }

        private void LoadInActiveCustomerReport()
        {
            DGVInactiveCustomer.DataSource =
                ReportBLL.GetReportAboutInActiveCustomerHire(Convert.ToInt32(ddlNumberOfDays.SelectedValue));
            DGVInactiveCustomer.DataBind();
        }
    }
}